import logging

import pytest

from resource.pom.hap import Hap
from utils.manager import Images


@pytest.fixture(autouse=True)
def uninstall_hap(device):
    yield
    try:
        device.uninstall_hap(Hap.bundle_name)
    except:
        pass


class Test:
    hap = Hap

    @pytest.mark.parametrize('setup_teardown', [None], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1：准备HAP包，cmd中执行安装HAP包的指令：hdc install xxx.hap，在设备的桌面查看安装结果')
        device.install_hap(self.hap.package_path)
        logging.info('预期结果1：桌面hap包显示安装正常，各功能运行正常，且不影响其他应用使用')
        device.refresh_layout()
        installed_hap = device.hdc_shell('bm dump -a')
        assert self.hap.bundle_name in installed_hap, '应用安装失败'
        show_name = device.get_element_by_text(self.hap.name)
        assert show_name, '应用名称未显示'
        x, y = device.center_of_element(show_name)
        device.click(x, y - 50)
        device.wait(10)
        current_page = device.save_snapshot_to_local('hap.jpeg')
        Images.crop_picture(current_page)
        similarity = Images.compare_image_similarity(current_page, self.hap.main_page)
        assert similarity > 0.8, '界面显示异常'
